using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;

namespace BlogControl
{
    class JavascriptManager
    {
        public static void WriteAllFunctions(HtmlTextWriter writer)
        {
            WriteCommentScripts(writer);
            WriteCurrentAction(writer);
            WriteHiddenPostId(writer);
            StringBuilder sb=new StringBuilder();
            AddSelectedByDateFunction(sb);
            writer.Write(sb.ToString());
        }
        /// <summary>
        /// show the add comment box
        /// </summary>
        /// <param name="writer"></param>
        public static void WriteCommentScripts(HtmlTextWriter writer)
        {
            if (FunctionRendered("showAddComment")) return;
            writer.Write("<script type=\"text/javascript\">");
            writer.Write("function showAddComment(id){");
            writer.Write("document.getElementById(id).style.display='block';");
            writer.Write("}");
            writer.Write("</script>");
        }

        /// <summary>
        /// write the hidden field which will hold the current action
        /// and the javascript function that will set that action
        /// </summary>
        /// <param name="writer"></param>
        public static void WriteCurrentAction(HtmlTextWriter writer)
        {
            if (FunctionRendered("setCurrentAction")) return;
            writer.Write(BuildHiddenField("hiddenActionName",null));
            writer.Write("<script type=\"text/javascript\">");
            writer.Write("function setCurrentAction(name){");
            writer.Write("document.getElementById('hiddenActionName').value=name;");
            writer.Write("}");
            writer.Write("</script>");
        }

        /// <summary>
        /// write the hidden field which will hold the current post id 
        /// and the javascript function that will set that id
        /// </summary>
        /// <param name="writer"></param>
        public static void WriteHiddenPostId(HtmlTextWriter writer)
        {
            if (FunctionRendered("setCurrentId")) return;

            writer.Write(BuildHiddenField("hiddenPostId",null));
            writer.Write("<script type=\"text/javascript\">");

            writer.Write("function setCurrentId(id){");
            writer.Write("document.getElementById('hiddenPostId').value=id;");
            writer.Write("}");
            writer.Write("</script>");
        }

        /// <summary>
        /// check if we've already wrote that function
        /// </summary>
        /// <param name="functionName"></param>
        /// <returns></returns>
        private static bool FunctionRendered(string functionName)
        {
            if (HttpContext.Current == null) return false;//design mode
            if (HttpContext.Current.Items["function_" + functionName] == null)
            {
                HttpContext.Current.Items.Add("function_" + functionName, "rendered");
                return false;
            }
            return true;
        }

        internal static int GetLastPageIndex()
        {
            if (HttpContext.Current.Request.Params["lastPageIndex"] != null)
            {
                return Convert.ToInt32(HttpContext.Current.Request.Params["lastPageIndex"]);
            }
            return 0;
        }

        internal static void SetOldPageIndex(StringBuilder writer,int oldPageIndex)
        {
            writer.Append(BuildHiddenField("lastPageIndex", oldPageIndex.ToString()));
        }

        internal static void AddSelectedByDateFunction(StringBuilder buffer)
        {
            if (FunctionRendered("selectPostsByDate")) return;
            buffer.AppendLine(BuildHiddenField("postsDateFilter", null));
            buffer.AppendLine("<script type=\"text/javscript\">");
            buffer.AppendLine("function selectPostsByDate(d){");
            buffer.AppendLine("document.getElementById('postsDateFilter').value=d;");
            buffer.AppendLine("}");
            buffer.AppendLine("</script>");
        }

        private static string BuildHiddenField(string id, string value)
        {
            return "<input type=\"hidden\" name=\"" + id + "\" id=\"" + id + "\" value=\"" + value + "\"/>";
        }
    }
}
